package com.fujimana.android.manacode;

import java.io.IOException;

import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fujimana.android.manacode.converter.Converter;
import com.fujimana.android.manacode.converter.ConverterFactory;
import com.fujimana.android.manacode.converter.Options;

public class SourceViewListener implements TabListener {

	private LinearLayout layout;
	
	private ManacodeActivity activity;
	
	private String source;
	
	private String id;
	
	private String type;
	
	SourceViewListener(LinearLayout layout, ManacodeActivity activity, String source, String path, String type) {
		this.layout = layout;
		this.activity = activity;
		this.source = source;
		this.id = path;
		this.type = type;
	}
	
	public Converter getConverter(String type, Options options) {
		return ConverterFactory.getInstance().getConverter(type, options);
	}
	
	private Options getOptions() {
		Options options = new Options();
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.activity);
		
		String tabSize= prefs.getString(this.activity.getString(R.string.settings_tabsize_key), "4");
		String fontSize= prefs.getString(this.activity.getString(R.string.settings_fontsize_key), "12");
		boolean lineno   = prefs.getBoolean(this.activity.getString(R.string.settings_lineni_key), false);
		String encoding= prefs.getString(this.activity.getString(R.string.settings_encoding_key), getEncoding());
		
		options.setTabSize(Integer.parseInt(tabSize));
		options.setFontSize(Integer.parseInt(fontSize));
		options.setLineno(lineno);
		options.setEncoding(encoding);
		
		return options;
	}
	
	private String getEncoding() {
		String def = System.getProperty("file.encoding");
		if (def == null || def.trim().equals("")) {
			def = "UTF-8";
		}
		
		return def;
	}
	
	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		
		Converter converter = getConverter(type, getOptions());
		
		String str = this.source;
		try {
			str = converter.convert(source);
		} catch (IOException e) {
			throw new ManacodeException(e);
		}
		
		LayoutInflater inf = this.activity.getLayoutInflater();
		inf.inflate(R.layout.source_view, this.layout);
		
		TextView textView = (TextView)this.layout.findViewById(R.id.source_title);
		textView.setText(this.id);
		
		WebView webView = (WebView) this.layout.findViewById(R.id.web_view);
		
		webView.getSettings().setJavaScriptEnabled(false);
		webView.setWebViewClient(new WebViewClient() {});
		
		webView.getSettings().setBuiltInZoomControls(true);
		
		webView.loadDataWithBaseURL("about:blank", str, "text/html", getOptions().getEncoding(), null);
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		if(this.layout.getChildCount()>0){
            this.layout.removeAllViews();
        }
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {	
	}


}
